package com.suixinsanren.design.state.demo.demo01;

import java.util.Random;

/**
 * 已投币状态
 */
public class HasQuarterState implements State {

    private final GumballMachine gumballMachine;
    private final Random random = new Random(System.currentTimeMillis());

    public HasQuarterState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }

    @Override
    public void insertQuarter() {
        System.out.println("你不能再投入硬币了，硬币已退还");
    }

    @Override
    public void ejectQuarter() {
        System.out.println("硬币已退还");
        gumballMachine.setState(gumballMachine.getNoQuarterState());
    }

    @Override
    public void turnCrank() {
        System.out.println("你转动了曲柄");
        int winter = random.nextInt(10);
        System.out.println(winter);
        if (winter == 0 && gumballMachine.getCount() > 0) {
            gumballMachine.setState(gumballMachine.getWinterState());
        } else {
            gumballMachine.setState(gumballMachine.getSoldState());
        }
    }

    @Override
    public void dispense() {
        System.out.println("已经没有糖果了");
    }
}
